package activity.addCamera;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.hichip.campro.R;
import base.HiActivity;
import butterknife.BindView;
import common.TitleView;
import main.MainActivity;
import utils.HiToast;
import utils.HiTools;
import zxing.CaptureActivity;

/**
 * 声波配置 准备界面 是否听到连接声音
 */
public class SoundWavesReadyActivity extends HiActivity implements View.OnClickListener {
    @BindView(R.id.title) TitleView title;
    @BindView(R.id.tv_hint) TextView tv_hint;
    @BindView(R.id.tv_next_step) TextView tv_next_step;
    @BindView(R.id.iv_anim_1) ImageView iv_anim_1;
    @BindView(R.id.iv_anim_2) ImageView iv_anim_2;
    @BindView(R.id.iv_anim_3) ImageView iv_anim_3;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_sound_waves_ready;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        initViewAndData();
        setListeners();

    }

    private void setListeners() {
        tv_hint.setOnClickListener(this);
        tv_next_step.setOnClickListener(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void initViewAndData() {
        title.setTitle(getString(R.string.soundwave_status_confirm));
        title.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
        title.setRightTxtBack(R.mipmap.x_bule);
        title.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int which) {
                switch (which){
                    case TitleView.NAVIGATION_BUTTON_LEFT:
                        finish();
                        break;
                    case TitleView.NAVIGATION_BUTTON_IV_RIGHT:
                        Intent intent=new Intent(SoundWavesReadyActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent,true);
                        break;
                }
            }
        });
        tv_hint.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tv_hint.getPaint().setAntiAlias(true);

        iv_anim_1.animate().alpha(0f).setDuration(0).start();
        iv_anim_2.animate().alpha(0f).setDuration(0).start();
        iv_anim_3.animate().alpha(0f).setDuration(0).start();
        handAnim();

    }

    private void handAnim() {
        ObjectAnimator anima_1=ObjectAnimator.ofFloat(iv_anim_1,"Alpha",0f,1f);
        anima_1.setDuration(400);
        anima_1.start();
        anima_1.addListener(new AnimatorListenerAdapter(){
            @Override
            public void onAnimationEnd(Animator animation) {
                ObjectAnimator anim_2=ObjectAnimator.ofFloat(iv_anim_2,"Alpha",0f,1f);
                anim_2.setDuration(400);
                anim_2.start();
                anim_2.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        ObjectAnimator anim_3=ObjectAnimator.ofFloat(iv_anim_3,"Alpha",0f,1f);
                        anim_3.setDuration(400);
                        anim_3.start();
                        anim_3.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                super.onAnimationEnd(animation);
                                iv_anim_1.animate().alpha(0f).setDuration(0).start();
                                iv_anim_2.animate().alpha(0f).setDuration(0).start();
                                iv_anim_3.animate().alpha(0f).setDuration(0).start();
                                mHandler.sendEmptyMessage(110);
                            }
                        });
                    }
                });
            }
        });
    }

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 110:
                    handAnim();
                    break;
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_hint://疑问解答
                handHint();
                break;
            case R.id.tv_next_step://下一步
                if(HiTools.isWifiConnected(SoundWavesReadyActivity.this)){
                    Intent intent=new Intent(SoundWavesReadyActivity.this, CaptureActivity.class);
                    intent.putExtra("category",5);
                    startActivity(intent);
                }else {
                    HiToast.showToast(SoundWavesReadyActivity.this,getString(R.string.toast_connect_wifi));
                }
                break;
        }
    }

    private void handHint() {
        View customView=View.inflate(SoundWavesReadyActivity.this, R.layout.pup_sound_waves_ready,null);
        PopupWindow popupWindow=new PopupWindow(customView);
        ColorDrawable cd = new ColorDrawable(-000);
        popupWindow.setBackgroundDrawable(cd);
        popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(customView, Gravity.CENTER, 0, 0);
        TextView tv_cancel=customView.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(mHandler!=null){
            mHandler.removeCallbacksAndMessages(null);
        }
        finish();
    }
}
